<template>
  <!-- 近三年财务信息 -->
  <div>
    <curd-title v-if="infoFlg" :title="'近三年财务信息'" />
    <avue-crud
      :table-loading="financeLoading"
      :option="financeOption"
      :data="financeList"
      v-model="financeForm"
      @on-load="onFinanceLoad"
      @row-save="rowFinanceSave"
      @row-update="rowFinanceUpdate"
      @refresh-change="refreshFinanceChange"
      @row-del="rowFinanceDel"
      :row-class-name="delRowClassName"
    >
      <template slot="year" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.yearFlag === '1'">{{ scope.row.year }}</del>
          {{ scope.row.yearA }}
        </div>
        <span v-else>{{ scope.row.year }}</span>
      </template>
      <template slot="totalAssets" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.totalAssetsFlag === '1'">{{ scope.row.totalAssets }}</del>
          {{ scope.row.totalAssetsA }}
        </div>
        <span v-else>{{ scope.row.totalAssets }}</span>
      </template>
      <template slot="totalLiabilities" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.totalLiabilitiesFlag === '1'">{{ scope.row.totalLiabilities }}</del>
          {{ scope.row.totalLiabilitiesA }}
        </div>
        <span v-else>{{ scope.row.totalLiabilities }}</span>
      </template>
      <template slot="currentAssets" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.currentAssetsFlag === '1'">{{ scope.row.currentAssets }}</del>
          {{ scope.row.currentAssetsA }}
        </div>
        <span v-else>{{ scope.row.currentAssets }}</span>
      </template>
      <template slot="currentLiabilities" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.currentLiabilitiesFlag === '1'">{{ scope.row.currentLiabilities }}</del>
          {{ scope.row.currentLiabilitiesA }}
        </div>
        <span v-else>{{ scope.row.currentLiabilities }}</span>
      </template>
      <template slot="businessIncome" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.businessIncomeFlag === '1'">{{ scope.row.businessIncome }}</del>
          {{ scope.row.businessIncomeA }}
        </div>
        <span v-else>{{ scope.row.businessIncome }}</span>
      </template>
      <template slot="grossProfit" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.grossProfitFlag === '1'">{{ scope.row.grossProfit }}</del>
          {{ scope.row.grossProfitA }}
        </div>
        <span v-else>{{ scope.row.grossProfit }}</span>
      </template>
      <template slot="netProfit" slot-scope="scope">
        <div v-if="slotFlg">
          <del v-if="scope.row.netProfitFlag === '1'">{{ scope.row.netProfit }}</del>
          {{ scope.row.netProfitA }}
        </div>
        <span v-else>{{ scope.row.netProfit }}</span>
      </template>
    </avue-crud>
  </div>
</template>

<script>
import { financeOption } from '@/option/tenant/supplier/finance-info'
import CurdTitle from '@/components/base/title'
import { getFinanceList, addFinance, removeFinance, updateFinance } from '@/api/supplier/finance'
import { getStore } from '@/util/store'
import delRowClassName from '@/views/plugin/workflow/mixins/info'

export default {
  name: 'FinanceInfo',
  components: { CurdTitle },
  inject: {
    type: {
      default: 'view'
    },
    infoFlg: {
      default: true
    },
    slotFlg: {
      default: false
    }
  },
  mixins: [delRowClassName],
  data() {
    return {
      supplierId: getStore({ name: 'supplierId' }),
      // 近三年财务信息
      financeLoading: false,
      financeOption: financeOption.call(this),
      financeForm: {},
      financeList: []
    }
  },
  methods: {
    // 近三年财务信息
    refreshFinanceChange() {
      this.onFinanceLoad()
    },
    onFinanceLoad() {
      this.infoFlg
        ? ((this.financeLoading = true),
          getFinanceList({ supplierId: this.supplierId }).then(res => {
            this.financeList = res.data.data
            this.financeLoading = false
          }))
        : ''
    },
    rowFinanceSave(row, done, loading) {
      addFinance({ ...row, supplierId: this.supplierId }).then(
        () => {
          this.onFinanceLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowFinanceUpdate(row, index, done, loading) {
      updateFinance(row).then(
        () => {
          this.onFinanceLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    rowFinanceDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return removeFinance(row.id)
        })
        .then(() => {
          this.onFinanceLoad()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    }
  }
}
</script>

<style></style>
